package ink.lovejinhu.leetcode;

import java.util.LinkedList;

/**
 * @Author: jin
 * @Date: 2021/9/13 17:40
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Solution_0922 {
    //    public int[] sortArrayByParityII(int[] nums) {
//        for (int i = 0, j = nums.length - 1; i < j; ) {
//            int lefti = ifOuShu(i);
//            int leftValue = ifOuShu(nums[i]);
//
//            int rightj = ifOuShu(j);
//            int rightValue = ifOuShu(nums[j]);
//
//            /**
//             * 两个都满足
//             */
//            if (lefti == leftValue && rightj == rightValue) {  //不用换
//                i++;
//                j--;
//                continue;
//            }
//            if (!(lefti == leftValue) && !(rightj == rightValue)) {  //交换两个
//                int temp = nums[i];
//                nums[i] = nums[j];
//                nums[j] = temp;
//                i++;
//                j--;
//                continue;
//            }
//
//            if (lefti == leftValue) {
//                i++;
//            } else {
//                j--;
//            }
//        }
//        return nums;
//    }
    public int[] sortArrayByParityII(int[] nums) {
        LinkedList<Integer> qi = new LinkedList<>();
        LinkedList<Integer> ou = new LinkedList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] % 2 == 0) {
                ou.add(nums[i]);
            } else {
                qi.add(nums[i]);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (i % 2 == 0) {
                nums[i] = ou.getFirst();
                ou.removeFirst();
            } else {
                nums[i] = qi.getFirst();
                qi.removeFirst();
            }
        }
        return nums;
    }

    public int ifOuShu(int i) {
        return i % 2 == 0 ? 0 : 1;
    }
}
